package com.hpin.fansea.my;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.MyOrderResult;
import com.hpin.fansea.bean.OrderReturn;
import com.hpin.fansea.bean.ResultBaseBean;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

/**
 * 退货申请页面
 * <p/>
 * Created by Aaron on 15/9/22.
 */
public class SaleReturnActivity extends BaseActivity {

    private MyOrderResult.OrderDetail orderDetail;
    private TextView order_shopName;
    private Button order_return_submit;
    private EditText order_return_reason;
    private TextView order_shopState;
    private ImageView order_image;
    private TextView order_title;
    private TextView order_totalPrice;
    private TextView order_payCash;
    private TextView order_totalNum;
    private TextView tv_buy_time;
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sale_return);

        orderDetail = (MyOrderResult.OrderDetail) getIntent().getSerializableExtra("orderDetail");

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_loading_default_list)
                .showImageForEmptyUri(R.mipmap.icon_fail_default_list)
                .showImageOnFail(R.mipmap.icon_fail_default_list)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

        initWidget();

        setOrderData();
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.order_detail));

        iv_left.setOnClickListener(this);

        // 商品名称
        order_shopName = (TextView) findViewById(R.id.order_shopName);
        // 提交按钮
        order_return_submit = (Button) findViewById(R.id.order_return_submit);
        // 退货理由
        order_return_reason = (EditText) findViewById(R.id.order_return_reason);
        // 商品状态
        order_shopState = (TextView) findViewById(R.id.order_shopState);
        // 商品图片
        order_image = (ImageView) findViewById(R.id.order_image);
        // 商品标题
        order_title = (TextView) findViewById(R.id.order_title);
        // 商品总价
        order_totalPrice = (TextView) findViewById(R.id.order_totalPrice);
        // 支付金额
        order_payCash = (TextView) findViewById(R.id.order_payCash);
        // 商品总数量
        order_totalNum = (TextView) findViewById(R.id.order_totalNum);
        // 下单时间
        tv_buy_time = (TextView) findViewById(R.id.tv_buy_time);

        order_return_submit.setOnClickListener(this);
    }


    /**
     * 设置订单数据
     */
    private void setOrderData() {

        if (orderDetail != null) {

            //是否可以退货
            switch (orderDetail.ordStat) {

                case Constant.ORDER_STATE_CANEL:// 无法退货的状态 ----已取消

                    order_shopState.setText("已取消");


                    break;
                case Constant.ORDER_STATE_RETURN_WAIT:// 无法退货的状态 ----退款待审核

                    order_shopState.setText("退款待审核");


                    break;
                case Constant.ORDER_STATE_SUBMIT_REFUND_SUCCESS:// 无法退货的状态 ---- 待退款

                    order_shopState.setText("待退款");

                    break;
                case Constant.ORDER_STATE_RETURN_REFUND_SUCCESS:// 无法退货的状态 ----已退款

                    order_shopState.setText("已退款");

                    break;

                case Constant.ORDER_STATE_NOT_PAY:

                    order_shopState.setText("未付款");
                    break;

                case Constant.ORDER_STATE_PAY:

                    order_shopState.setText("已付款");
                    break;

                case Constant.ORDER_STATE_VALID:

                    order_shopState.setText("有效订单");
                    break;

                case Constant.ORDER_STATE_ARRIVE:

                    order_shopState.setText("到货订单");
                    break;

                case Constant.ORDER_STATE_FINISH:

                    order_shopState.setText("已完成");
                    break;

                case Constant.ORDER_STATE_SUBMIT_REFUND_FAIL:

                    order_shopState.setText("审核不通过");
                    break;

            }

            order_shopName.setText("订单号:" + orderDetail.ordNo);

            // 购买时间
            tv_buy_time.setText(orderDetail.createTime2);

            MyOrderResult.OrderDetail.GroupBuy groupBuy = orderDetail.groupBuy;

            if (groupBuy != null) {

                order_title.setText(groupBuy.name);// shop 标题
                ImageLoader.getInstance().displayImage(groupBuy.fmpica, order_image, options);// shop 图片

            }

            MyOrderResult.OrderDetail.OrderDelv orderDelv = orderDetail.orderDelv;

            if (orderDelv != null) {

                order_totalPrice.setText("￥" + orderDetail.ordCash);// shop 价格 //订单金额

                order_payCash.setText("￥" + orderDetail.payCash);// shop //实际支付金额
//                order_redPacketCash.setText("￥" + orderDetail.getRedPacketCash());// shop 红包
                order_totalNum.setText("X" + orderDetail.groupBuyNum);// shop 数量

            }

        }

    }


    /**
     * 提交退货申请
     */
    private void commitSaleReturn() {

        if (orderDetail == null) {
            showToast("未获取到商品详细信息");
            return;
        }

        // 团购退货
        // 拼接支付json
        OrderReturn orderReturn = new OrderReturn();
        orderReturn.orderId = orderDetail.id_;
        orderReturn.backCash = orderDetail.payCash;
        orderReturn.backReason = order_return_reason.getText().toString();
        String backOrder = JSON.toJSONString(orderReturn);
        RequestParams params = new RequestParams();
        params.addBodyParameter("token", Constant.token);
        params.addBodyParameter("backOrder", backOrder);
        params.addBodyParameter("systemType", Constant.SYSTEM_TYPE);

        HttpUtil.getInstance().setPostRequest("提交中...", this, Constant.WEB_ROOT + Constant.SALE_RETURN, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                    if (Constant.STATUS_SUCESS.equals(resultBaseBean.status)) {

                        showToast("提交成功");

                        Intent mIntent = new Intent(SaleReturnActivity.this, MyOrderActivity.class);
                        startActivity(mIntent);
                        finish();

                    }else {
                        showToast(resultBaseBean.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("提交失败");
                }

            }
        } , null);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

            case R.id.order_return_submit:

                if (CommonUtils.isNull(order_return_reason.getText().toString().trim())) {
                    showToast("亲，退款理由不能为空哦");
                    return;
                }

                if (order_return_reason.getText().toString().trim().length() < 8 || order_return_reason.getText().toString().trim().length() > 200) {
                    showToast("亲，退款理由8-200个字符");
                    return;
                }

                commitSaleReturn();

                break;
        }
    }

}
